package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.R;
import com.atguigu.eduservice.entity.EduCourse;
import com.atguigu.eduservice.entity.vo.CourseInfoVo;
import com.atguigu.eduservice.entity.vo.CoursePublishVo;
import com.atguigu.eduservice.service.EduCourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author thomasyang
 * @since 2023-12-09
 */
@Api
//@CrossOrigin
@RestController
@RequestMapping("/eduservice/course")
public class EduCourseController {

    @Autowired
    private EduCourseService courseService;



    //课程列表 基本实现
    //TODO  完善条件查询带分页
    @ApiOperation(value = "分页查询课程")
    @GetMapping("getPageCourse")
    public R getPageCourseList(){
        List<EduCourse> list = courseService.list(null);

        return R.ok().data(list);
    }



    //添加课程基本信息
    @ApiOperation(value = "添加课程信息")
    @PostMapping("addCourseInfo")
    private R addCourse(@RequestBody CourseInfoVo  courseInfoVo){
        //返回添加之后课程id，为了后面添加大纲使用
        String id = courseService.saveCourse(courseInfoVo);

        return  R.ok().data("courseId", id);

    }

    @ApiOperation(value = "根据课程id查询课程信息")
    @GetMapping("getCourseInfo/{id}")
    public R getCourseInfo(@PathVariable String id){

        CourseInfoVo courseInfo = courseService.getCourseInfo(id);

        return R.ok().data("courseInfoVo", courseInfo);
    }

    @ApiOperation(value = "根据id更新课程信息")
    @PostMapping("updateCourseInfo")
    public R updateCourseInfo(@RequestBody  CourseInfoVo courseInfoVo){

        Boolean flag =  courseService.updateCourseInfo(courseInfoVo);

        return R.flagCheck(flag);

    }


    @ApiOperation(value = "根据id获取课程发布信息")
    @GetMapping("getPublishCourseInfo/{id}")
    public R getCoursePublishVoById(@PathVariable String id){

        CoursePublishVo coursePublishVo =  courseService.getCoursePublishInfoVoById(id);
        System.out.println(coursePublishVo);
        return R.ok().data(coursePublishVo);
    }

    @ApiOperation(value = "发布课程")
    @PostMapping("publishCourse/{id}")
    public R publishCourseById(@PathVariable String id){

        Boolean flag =  courseService.publishById(id);
        return  R.flagCheck(flag);
    }

    @ApiOperation(value = "删除课程")
    @DeleteMapping("delete/{id}")
    public R deleteCourse(@PathVariable String id){

        Boolean flag =  courseService.removeCourse(id);
        return R.flagCheck(flag);
    }






}

